package com.config;

import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.metadata.EndPoint;
import com.datastax.oss.driver.internal.core.metadata.DefaultEndPoint;
import com.domain.vote.Address;
import com.google.common.collect.Lists;
import org.springframework.boot.autoconfigure.cassandra.CqlSessionBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.cassandra.core.CassandraAdminTemplate;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.core.convert.CassandraCustomConversions;
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
import org.springframework.data.cassandra.core.mapping.Table;
import org.springframework.data.cassandra.core.mapping.UserTypeResolver;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;

import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Collections;

@EnableCassandraRepositories(basePackages = "com.repository")
@Configuration
public class CassandraConfig {

    @Bean
    public CqlSessionBuilderCustomizer builderCustomizers(){
        return (CqlSessionBuilder cql)->{
          cql.withLocalDatacenter("datacenter1").withKeyspace("article");
        };
    }
    private static class UDTToAddressConverter implements Converter<UdtValue, Address> {

        @Override
        public Address convert(UdtValue source) {
            return new Address(source.getString("country"),source.getString("city"),source.getString("village"));
        }
    }

    private static class AddressToUDTConverter implements Converter<Address, UdtValue> {

        private final UserTypeResolver userTypeResolver;

        AddressToUDTConverter(UserTypeResolver userTypeResolver) {
            this.userTypeResolver = userTypeResolver;
        }

        @Override
        public UdtValue convert(Address source) {
            com.datastax.oss.driver.api.core.type.UserDefinedType userType = userTypeResolver
                    .resolveType(CqlIdentifier.fromCql("address"));
            UdtValue udtValue = userType.newValue();
            udtValue.setString("country", source.getCountry());
            udtValue.setString("city", source.getCity());
            udtValue.setString("village", source.getVillage());

            return udtValue;
        }
    }
    
    @Bean
    public CassandraCustomConversions cassandraCustomConversions(CqlSession cqlSession) {

        UDTToAddressConverter udtToAddressConverter = new UDTToAddressConverter();
        AddressToUDTConverter addressToUDTConverter = new AddressToUDTConverter(new SimpleUserTypeResolver(cqlSession));
        return new CassandraCustomConversions(Lists.newArrayList(udtToAddressConverter,addressToUDTConverter));
    }
//    CassandraCustomConversions cassandraCustomConversions = new CassandraCustomConversions(
//            Arrays.asList(new UDTToAddressConverter(), new AddressToUDTConverter(userTypeResolver)));

//    @Bean
//    public CassandraTemplate cassandraTemplate(CqlSession cqlSession){
//        return new CassandraTemplate(cqlSession);
//    }
//    @Bean
//    public CassandraAdminTemplate cassandraAdminTemplate(CqlSession cqlSession){
//        CassandraAdminTemplate cassandraAdminTemplate = new CassandraAdminTemplate(cqlSession);
//        return cassandraAdminTemplate;
//    }

}

